package com.yunti.springboot.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunti.springboot.bo.UserBO;
import com.yunti.springboot.config.component.AbstractCurrentRequestComponent;
import com.yunti.springboot.mapper.RoleMapper;
import com.yunti.springboot.po.Role;
import com.yunti.springboot.service.IRoleService;
import com.yunti.springboot.vo.Result;
import com.yunti.springboot.vo.req.RoleAddReq;
import com.yunti.springboot.vo.req.RoleReq;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户角色关联表 服务实现类
 * </p>
 *
 * @author kmustleng
 * @since 2022-12-09
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {
    @Resource
    RoleMapper mapper;

    @Resource
    private AbstractCurrentRequestComponent abstractCurrentRequestComponent;

    @Override
    public List<Map<String, String>> getRuleList() {
        return mapper.getRuleList();
    }

    @Override
    public Result getRulePageList(RoleReq req) {
        if (req.getCurrent() == null) {
            req.setCurrent(0);
        }
        if (req.getSize() == null) {
            req.setSize(10);
        }
        Page page = new Page<>(req.getCurrent(), req.getSize());
        return Result.success(page.setRecords(mapper.getSchoolListPage(req,page)));
    }

    @Override
    public List<Map<String, String>> getOwnRuleList() {
        UserBO userBO = abstractCurrentRequestComponent.getCurrentStaffId();
        return mapper.getOwnRuleList(userBO.getId());
    }

    @Override
    public Result addRole(RoleAddReq req) {
        Role role = new Role()
                .setCreateTime(LocalDateTime.now())
                .setUpdateTime(LocalDateTime.now())
                .setDescription(req.getDescription())
                .setName(req.getName());
        if(mapper.insert(role)>0){
            return Result.success("新增角色成功！");
        }
        return Result.error("","新增角色失败！");
    }

    @Override
    public Result editRole(RoleReq req) {
        Role role = new Role()
                .setId(req.getId())
                .setUpdateTime(LocalDateTime.now())
                .setDescription(req.getDescription())
                .setName(req.getName());
        if(mapper.updateById(role)>0){
            return Result.success("更新角色成功！");
        }
        return Result.error("更新角色失败！");
    }

    @Override
    public Result delRole(RoleReq req) {
        if(mapper.deleteById(new Role().setId(req.getId()))>0){
            return Result.success("删除角色成功！");
        }
        return Result.error("删除角色失败！");
    }
}
